<template>
  <div>
    <el-row :gutter="0">
      <el-col :span="24">
        <el-form :inline="true" label-width="80px" :model="table.queryForm">
          <el-form-item :label="textColumnComment">
            <el-input
              v-model="table.queryForm[props.textColumnKey]"
              :placeholder="textColumnComment"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="refresh-left" type="warning" @click="handleResetQuery">
              重置
            </el-button>
            <el-button icon="Search" type="primary" @click="queryTableData"> 查询 </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table
      ref="tableRef"
      v-loading="table.loading"
      border
      stripe
      :data="table.dataList"
      :height="table.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" width="50" />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in columnList"
        :key="index"
        align="center"
        :label="item.columnComment"
        :prop="item.columnKey"
        show-overflow-tooltip
        width="140"
        sortable
      >
        <template #default="{ row }">
          <div
            v-if="
              item.convertConfig && Object.prototype.hasOwnProperty.call(item.convertConfig, 'type')
            "
          >
            {{ row[item.convertConfig.targetColumnKey] }}
          </div>
          <div v-else>{{ row[item.columnKey] }}</div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="table.pagination.current"
      :layout="table.pagination.layout"
      :page-size="table.pagination.size"
      :total="table.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>
<script setup lang="ts">
import type { DefColumn } from "@/views/def/table/DefColumn";
import { queryList } from "@/api/def/columnctrl";
import { queryPage } from "@/api/def/tabledata";
import { useTable, BasicTableProps } from "@/hooks/table";
import { onMounted, reactive, ref } from "vue";

const props = defineProps({
  multiple: {
    type: Boolean,
    default: false
  },
  sourceTableId: {
    type: String,
    default: ""
  },
  idColumnKey: {
    type: String,
    default: "id"
  },
  textColumnKey: {
    type: String,
    default: "text"
  }
});

const tableRef = ref(null);
const queryFormData = ref({});
const columnList = ref<DefColumn[]>([]);
const textColumnComment = ref("");

onMounted(() => {
  handleToggleHeight(40);
  if (props.sourceTableId) {
    queryFormData.value["tableId"] = props.sourceTableId;
  }
  if (props.textColumnKey) {
    queryFormData.value[props.textColumnKey] = "";
  }
  queryColumnDataList();
});

const queryColumnDataList = () => {
  console.log(props.sourceTableId);
  queryList({ tableId: props.sourceTableId, columnShow: true }).then(res => {
    if (res.code === 200) {
      columnList.value = res.data;
      const textColumnIndex = columnList.value.findIndex(n => n?.columnKey === props.textColumnKey);
      if (textColumnIndex > -1) {
        textColumnComment.value = columnList.value[textColumnIndex]?.columnComment;
      }
    }
  });
};

const tableOptions = reactive<BasicTableProps>({
  multiple: props.multiple,
  queryForm: queryFormData.value,
  queryFunction: queryPage,
  descs: ["create_time"]
});

const {
  instance: table,
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

const getSelections = () => {
  return table.dataSelections;
};

defineExpose({
  getSelections
});
</script>
<style lang="scss" scoped></style>
